﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace wwork8
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
            InitUI();
        }
        private void InitUI()
        {
            // 初始化 ListView 显示详情
            listView1.View = View.Details;
            listView1.Columns.Add("文件名", 300);
            listView1.Columns.Add("大小 (字节)", 100);

            // 事件绑定
            this.Load += Form1_Load;
            treeView1.BeforeExpand += TreeView1_BeforeExpand;
            treeView1.AfterSelect += TreeView1_AfterSelect;
            listView1.DoubleClick += ListView1_DoubleClick;

          
        }
        // 窗体加载时加载磁盘根目录
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDrives();
        }

        // 加载磁盘驱动器
        private void LoadDrives()
        {
            treeView1.Nodes.Clear();
            foreach (var drive in DriveInfo.GetDrives())
            {
                TreeNode driveNode = new TreeNode(drive.Name);
                driveNode.Tag = drive.RootDirectory.FullName;
                driveNode.Nodes.Add("..."); // 占位，延迟加载
                treeView1.Nodes.Add(driveNode);
            }
        }

        // 目录展开时加载子目录
        private void TreeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == "...")
            {
                e.Node.Nodes.Clear();
                try
                {
                    var dirs = Directory.GetDirectories(e.Node.Tag.ToString());
                    foreach (var dir in dirs)
                    {
                        TreeNode node = new TreeNode(Path.GetFileName(dir));
                        node.Tag = dir;
                        node.Nodes.Add("...");
                        e.Node.Nodes.Add(node);
                    }
                }
                catch
                {
                    // 可能没有权限访问目录，忽略异常
                }
            }
        }

        // 选中目录时显示文件
        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            LoadFiles(e.Node.Tag.ToString());
        }

        // 加载文件到 ListView
        private void LoadFiles(string path)
        {
            listView1.Items.Clear();
            try
            {
                var files = Directory.GetFiles(path);
                foreach (var file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    ListViewItem item = new ListViewItem(fi.Name);
                    item.SubItems.Add(fi.Length.ToString());
                    item.Tag = file; // 保存完整路径
                    listView1.Items.Add(item);
                }
               
            }
            catch
            {
                // 可能无权限
            }
        }

        // 双击文件，判断是否为XML文件，解析展示根节点
        private void ListView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                var filePath = listView1.SelectedItems[0].Tag as string;
                if (filePath != null && Path.GetExtension(filePath).Equals(".xml", StringComparison.OrdinalIgnoreCase))
                {
                    ParseXmlFile(filePath);
                }
            }
        }

        // 解析 XML 文件并弹窗显示根节点名
        private void ParseXmlFile(string filePath)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                MessageBox.Show($"XML文件根节点: {doc.DocumentElement.Name}", "XML解析");
            }
            catch (Exception ex)
            {
                MessageBox.Show("解析失败：" + ex.Message, "错误");
            }
        }

        // 菜单退出
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // 工具栏刷新
        private void ToolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                var path = treeView1.SelectedNode.Tag.ToString();
                LoadFiles(path);
                // 也可以刷新目录树，比如重新加载选中节点的子目录
                treeView1.SelectedNode.Nodes.Clear();
                treeView1.SelectedNode.Nodes.Add("...");
                treeView1.SelectedNode.Expand();
            }
            else
            {
                LoadDrives();
                listView1.Items.Clear();
            }
        }
    }
}
